package com.dongjiantong.web.protal.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.sys.model.AreaWebModule;
import com.dongjiantong.sys.model.WebModule;
import com.dongjiantong.sys.service.AreaWebModuleService;
import com.dongjiantong.sys.service.WebModuleService;
import com.dongjiantong.user.model.*;
import com.dongjiantong.user.service.*;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.shaded.com.google.common.collect.Maps;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by ldf on 2018/1/16.
 */
@OpenBean
@Slf4j
public class WebModuleController {
    @Reference(version = "1.0.0")
    private WebModuleService webModuleService;
    @Reference(version = "1.0.0")
    private AreaWebModuleService areaWebModuleService;
    @Reference(version = "1.0.0")
    private OrganizationUserService organizationUserService;
    @Reference(version = "1.0.0")
    private OrganizationService organizationService;
    @Reference(version = "1.0.0")
    private RegionAccountService regionAccountService;

    /**
     * 查询开户地区所显示模块code
     *
     * @param userId
     * @return
     */
    @OpenMethod(key = "web.AreaWebModule.selectWebModuleCode",
            paramNames = {"userId"},
            httpMethods = RequestMethod.GET)
    public Response<List> selectWebModuleCode(Long userId) {
        //查询单位
        OrganizationUser organizationUser = organizationUserService.queryOrganizationByUserId(userId);
        Organization organization = organizationService.selectByPrimaryKey(organizationUser.getOrgId());
        //查询单位所在机构地区
        RegionAccount regionAccount = regionAccountService.selectByPrimaryKey(organization.getRegionAccountId());
        //县级以下按县级配置
        if (regionAccount.getLevel() > 3) {
            RegionAccount regionAccount1 = new RegionAccount();
            regionAccount1.setLevel(3);
            regionAccount1.setProvinceId(regionAccount.getProvinceId());
            regionAccount1.setCityId(regionAccount.getCityId());
            regionAccount1.setRegionId(regionAccount.getRegionId());
            RegionAccount regionAccountResult = regionAccountService.selectOne(regionAccount1);

            AreaWebModule areaWebModule = new AreaWebModule();
            areaWebModule.setRegionAccountId(regionAccountResult.getId());
            List<AreaWebModule> areaWebModuleList = areaWebModuleService.select(areaWebModule);
            List<String> stringList = new ArrayList<>();

            for (AreaWebModule areaWebModule1 : areaWebModuleList) {
                stringList.add(areaWebModule1.getWebModuleNumber());
            }
            return Response.ok(stringList);

        }else {//县级及县级以上按单位所在开户地区配置
            AreaWebModule areaWebModule = new AreaWebModule();
            areaWebModule.setRegionAccountId(regionAccount.getId());
            List<AreaWebModule> areaWebModuleList = areaWebModuleService.select(areaWebModule);
            List<String> stringList = new ArrayList<>();

            for (AreaWebModule areaWebModule1 : areaWebModuleList) {
                stringList.add(areaWebModule1.getWebModuleNumber());
            }
            return Response.ok(stringList);
        }


    }

}
